package com.topica;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.topica.adapter.AdapterCourseUpdate;
import com.topica.database.callback.OnGetLinkDownloadListener;
import com.topica.database.task.GetLinkDownloadDataCourse;
import com.topica.network.GetCourseTask;
import com.topica.network.GetListUpdate;
import com.topica.network.UpdateCourse;
import com.topica.network.callback.OnDownloadListener;
import com.topica.network.callback.OnGetCourseListener;
import com.topica.network.callback.OnGetListCourseUpdateListener;
import com.topica.network.callback.OnUnzipListener;
import com.topica.network.download.DownloadFileTask;
import com.topica.network.download.UnzipTask;
import com.topica.object.CourseUpdate;

public class TopicaUpdate extends Activity implements
		OnGetListCourseUpdateListener, OnItemClickListener,
		OnGetCourseListener, OnGetLinkDownloadListener, OnDownloadListener,
		OnUnzipListener {
	private ListView listUpdate;
	private boolean hasLoad;
	private String currentCourseId;
	private AdapterCourseUpdate adapterCourseUpdate;
	File rootDir = Environment.getExternalStorageDirectory();
	String directory = rootDir + "/topica/";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_list_course_update);
		listUpdate = (ListView) findViewById(R.id.listUpdate);
		adapterCourseUpdate = new AdapterCourseUpdate(getApplicationContext(),
				-1, -1, new ArrayList<CourseUpdate>());
		listUpdate.setAdapter(adapterCourseUpdate);
		listUpdate.setOnItemClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!hasLoad) {
			String username = ((TopicaApplication) getApplication())
					.getUserId();
			GetListUpdate update = new GetListUpdate(this, username);
			update.setOnGetListCourseUpdateListener(this);
			update.execute();
		}
	}

	@Override
	public void onGetListCourseUpdateListener(ArrayList<CourseUpdate> data) {
		if (data.isEmpty()) {
			Toast.makeText(getApplicationContext(), "Up to date",
					Toast.LENGTH_SHORT).show();
			finish();
		}
		hasLoad = true;
		for (CourseUpdate item : data) {
			adapterCourseUpdate.add(item);
		}
		adapterCourseUpdate.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		String courseId = ""
				+ adapterCourseUpdate.getItem(position).getCourseId();
		currentCourseId =  courseId;
		GetCourseTask task = new GetCourseTask(this, courseId);
		task.setOnGetCourseListener(this);
		task.execute();
	}

	@Override
	public void onGetCourseListener(String courseId) {
		GetLinkDownloadDataCourse course = new GetLinkDownloadDataCourse(this,
				courseId);
		course.setOnGetLinkDownloadListener(this);
		course.execute();
	}

	@Override
	public void onGetLinkDownloadListener(String url) {
		Toast.makeText(getApplicationContext(), "LINK = " + url,
				Toast.LENGTH_SHORT).show();
		DownloadFileTask task = new DownloadFileTask(TopicaUpdate.this);
		task.setOnDownloadListener(this);
		task.execute(url);
	}

	@Override
	public void onDownloadComplete(String fileName) {
		UnzipTask task = new UnzipTask(TopicaUpdate.this, fileName);
		task.setUnzipListener(this);
		task.execute(directory);
	}

	@Override
	public void onDownloadError() {
		Toast.makeText(this.getApplicationContext(), "Download failed",
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		this.setResult(RESULT_CANCELED);
		finish();
	}

	@Override
	public void onUnzipSuccess(String fileName) {
		String title_getdata_done = getResources().getString(
				R.string.title_getdata_done);
		File f = new File(directory + fileName);
		f.delete();
		String userId = ((TopicaApplication)getApplication()).getUserId();
		UpdateCourse course = new UpdateCourse(this, userId, currentCourseId);
		course.execute();
		Toast.makeText(this.getApplicationContext(), title_getdata_done,
				Toast.LENGTH_SHORT).show();
		this.setResult(RESULT_OK);
		finish();
	}

	@Override
	public void onUnzipFailed() {
		this.setResult(RESULT_CANCELED);
		finish();
	}
}
